package com.jeecg.p3.system.util;

import javax.servlet.http.HttpServletRequest;

public class HttpRequestDeviceUtils {
	private static String[] mobileGateWayHeaders = {
			"ZXWAP", "chinamobile.com", "monternet.com", "infoX", "XMS 724Solutions HTG",
			"wap.lizongbo.com", "Bytemobile" };

	private static String[] pcHeaders = {
			"Windows 98", "Windows ME", "Windows 2000", "Windows XP", "Windows NT", "Ubuntu" };

	private static String[] mobileUserAgents = {
			"Nokia", "SAMSUNG", "MIDP-2", "CLDC1.1", "SymbianOS", "MAUI", "UNTRUSTED/1.0",
			"Windows CE", "iPhone", "iPad", "Android", "BlackBerry", "UCWEB", "ucweb", "BREW",
			"J2ME", "YULONG", "YuLong", "COOLPAD", "TIANYU", "TY-", "K-Touch", "Haier", "DOPOD",
			"Lenovo", "LENOVO", "HUAQIN", "AIGO-", "CTC/1.0", "CTC/2.0", "CMCC", "DAXIAN", "MOT-",
			"SonyEricsson", "GIONEE", "HTC", "ZTE", "HUAWEI", "webOS", "GoBrowser", "IEMobile",
			"WAP2.0" };

	public static boolean isMobileDevice(HttpServletRequest request) {
		boolean pcFlag = false;
		boolean mobileFlag = false;
		String via = request.getHeader("Via");
		String userAgent = request.getHeader("user-agent");
		for (int i = 0; (via != null) && (!via.trim().equals(""))
				&& (i < mobileGateWayHeaders.length); i++) {
			if (via.contains(mobileGateWayHeaders[i])) {
				mobileFlag = true;
				break;
			}
		}
		for (int i = 0; (!mobileFlag) && (userAgent != null) && (!userAgent.trim().equals(""))
				&& (i < mobileUserAgents.length); i++) {
			if (userAgent.contains(mobileUserAgents[i])) {
				mobileFlag = true;
				break;
			}
		}
		for (int i = 0; (userAgent != null) && (!userAgent.trim().equals(""))
				&& (i < pcHeaders.length); i++) {
			if (userAgent.contains(pcHeaders[i])) {
				pcFlag = true;
			}
		}
		if ((mobileFlag) && (mobileFlag != pcFlag)) {
			return true;
		}
		return false;
	}
}
